package com.springboot.demo.mapper;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.springboot.demo.entity.Role;
import com.springboot.demo.entity.SystemLog;
import com.springboot.demo.entity.User;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.List;


/**
 * @author wubc683
 * @desc
 * @date 2021-07-23 14:51
 */
public interface UserMapper {

    @Select("select id,username,phone,parentId,rid,company,companyAddr,industryType,lastLoginTime,createDate,expireDate," +
            "isEnabled ,isLocked ,recordCount  ,deleteFlag from web_user where username = #{username} and deleteFlag " +
            "= ''")
    User getUserByUsername(String username);

    @Select("select id,username,phone,parentId,rid,company,companyAddr,industryType,lastLoginTime,createDate,expireDate," +
            "isEnabled ,isLocked ,recordCount  ,deleteFlag from web_user where phone = #{phone} and parentId = 0 and " +
            "deleteFlag =" + " ''")
    User getUserByPhone(String phone);

    @Update("update web_user set password = #{password} where phone = #{phone} and deleteFlag = ''")
    void updatePasswordByPhone(String phone, String password);

    @Select("select id,username,password,phone,parentId,rid,company,companyAddr,industryType,lastLoginTime," +
            "createDate,expireDate,isEnabled ,isLocked ,isPhoneChecked,recordCount  ,deleteFlag from web_user where " +
            "id = #{id} " + "and " + "deleteFlag = ''")
    User getUserById(Long id);

    @Insert("insert into web_user_role(uid,rid) values(#{uid},#{rid})")
    void insertUserRoleByUidAndRid(Long uid, Long rid);

    @Update("update web_user set password = #{password} where id = #{id}")
    void updatePasswordById(Long id, String password);

    @Select("select id,username,phone,parentId,rid,company,companyAddr,industryType,lastLoginTime,createDate," +
            "expireDate," + "isEnabled ,isLocked ,isPhoneChecked,recordCount ,deleteFlag from web_user where parentId" +
            " = #{id} and " + "deleteFlag = ''")
    List<User> getUserByParentId(Long id);

    @Delete("delete from web_user_role where uid = #{id}")
    void deleteUserRoleByUid(Long id);

    @Update("update web_user_role set rid = #{rid} where uid = #{id}")
    void updateUserRoleById(Long id, Long rid);

    @Update("update web_user set username = #{username},phone=#{phone}  where " + "id" + " = #{id}")
    void updateUserByNameAndPhone(Long id, String username, String phone);

    @Insert("insert into web_system_log(username,operator,browser,ip,loginTime) values(#{username},#{operator}," +
            "#{browser},#{ip},#{loginTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void insertSystemLog(SystemLog systemLog);

    @Insert("insert into web_user_system_log(uid,sid) values(#{uid},#{sid})")
    void insertUserSystemLog(Long uid, Long sid);

    @Select("select s.id,s.username,s.operator,s.browser,s.ip,s.loginTime from web_system_log s,web_user_system_log " +
            "us where s.id = us.sid and us.uid in (select id " +
            "from web_user where id = #{id} or parentId = #{id}) order by loginTime desc")
    List<SystemLog> getSystemLogByUid(Long id);

    @Select("select id,name,description from web_user_role ur, web_role r where ur.rid = r.id and ur.uid = #{uid} and" +
            " r.name = #{name}")
    Role selectUserRoleByUidAndRoleName(Long uid, String name);

    @Update("update web_user_role ur left join web_user u on ur.uid = u.id set ur.rid = #{u} where ur.rid = #{rid}" +
            " and u.parentId != 0")
    void updateRelativeUserRoleByUidAndRid(Long rid, Long u);

    @Delete("delete from web_user_role where uid = #{uid} and rid = #{rid}")
    void deleteUserRoleByUidAndRid(Long uid, Long rid);

    @Update("update web_user set username = #{username}, password = #{password}, phone = #{phone}, rid = " +
            "#{rid}, lastLoginTime = #{lastLoginTime},parentId = #{parentId},createDate = #{createDate} where " +
            "id = #{id}")
    void updateUser(User user);

    @Update("update web_user as a set isEnabled = 0 where a.id in (select id from web_user as u where id = #{u.id} or" +
            " (id = #{u.parentId} and deleteFlag = ''))")
    void updateUserDisabled(Long id);

    @Insert("insert into web_user(username,password,parentId,rid,phone,createDate,expireDate,company,companyAddr," +
            "industryType,isEnabled,isLocked,isPhoneChecked,recordCount,deleteFlag) values(#{username},#{password}," +
            "#{parentId}," + "#{rid}," +
            "#{phone},#{createDate},#{expireDate}," + "#{company},#{companyAddr},#{industryType},#{isEnabled}," +
            "#{isLocked},#{isPhoneChecked},#{recordCount},#{deleteFlag})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void saveUser(User user);

    @Update("update web_user set phone = #{phone} where id = #{id}")
    void updatUserPhoneById(Long id, String phone);

    @Insert("insert into web_user(username,password,phone,rid,parentId,createDate,expireDate,isEnabled,isLocked," +
            "isPhoneChecked,recordCount,deleteFlag) values(#{username},#{password},#{phone},#{rid},#{parentId}," +
            "#{createDate}," + "#{expireDate}," + "#{isEnabled},#{isLocked},#{isPhoneChecked},#{recordCount},#{deleteFlag})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void saveRelativeUser(User user);

    @Update("update web_user set phone = #{phone} ,isPhoneChecked = #{isPhoneChecked} where id = #{id}")
    void updateUserPhoneAndState(Long id, String phone, Boolean isPhoneChecked);

    @Update("update web_user set username = #{username},password = #{password},rid=#{rid},phone = #{phone}," +
            "isPhoneChecked = " + "#{isPhoneChecked}  where id = #{id}")
    void updateRelativeUser(User user);

    @Select("<script> select distinct username from web_user " +
            " <if test='deviceClusterId != null' > left join web_user_device_cluster on web_user.id = " +
            "web_user_device_cluster.userId and deviceClusterId = #{deviceClusterId} </if>" +
            "where (web_user.id = #{adminId} or (parentId = #{adminId} and " +
            "deleteFlag = '')) and web_user.id != #{ownerId}" + "<if test='username !=null and username != \"\"'>  " +
            "and " + "username " + "like concat('%', " + "#{username},'%') </if> " +
            "<if test='deviceClusterId != null' > and deviceClusterId is null </if>" +
            "</script>")
    List<String> searchUserByName(Long adminId, Long ownerId,String username, Long deviceClusterId);

    @Select("select id,username,phone,parentId,rid,company,companyAddr,industryType,lastLoginTime,createDate," +
            "expireDate," + "isEnabled ,isLocked ,isPhoneChecked,recordCount ,deleteFlag from web_user where parentId" +
            " = 0 and " + "deleteFlag = ''")
    List<User> getAllAdminUser();

    @Update("update web_user set isEnabled = #{isEnabled},expireDate = #{expireDate} where id = #{id}")
    void updateUserIsEnabledAndExpireDate(Long id, boolean isEnabled, Date expireDate);

    @Update("update web_user set isLocked = #{locked} where id = #{id} or (parentId = #{id} and deleteFlag = '')")
    void updateUserIsLocked(Long id, Integer locked);

    @Update("update web_user set expireDate = #{expireDate} where id = #{id} or (parentId = #{id} and deleteFlag = '')")
    void updateUserExpireDate(Long id, Date expireDate);

    @Select("select password from T_User where account = #{username} and deleteFlag = ''")
    String getMicoUserPwdByName(String username);

    @Select("select rid from web_user_role where uid = #{userId}")
    List<Long> getRoleIdByUserId(Long userId);

    @Update("update web_user set recordCount = #{count} where deleteFlag = '' and id = #{userId} or parentId = " +
            "#{userId}")
    void setUserAlamrRecordCount(Long userId, Integer count);

    @Update("update web_user set lastLoginTime = #{lastLoginTime} where id = #{id}")
    void updateUserLoginTime(User userInfoById);

//    @Select("select 1 from web_user where phone = #{phone}")
//    Integer selectPhone(String phone);

    @Update("update web_user set deleteFlag = #{deleteFlag} where id = #{userId}")
    void deleteUserByUserId(Long userId, String deleteFlag);

    @Update("update web_user set rid = #{user} where rid = #{rid} and deleteFlag = ''")
    void updateRelativeUserByUidAndRid(Long rid, Long user);

    @Select("select id from web_user where parentId = #{userId} and deleteFlag = ''")
    List<Long> getRelativeUserIdByAdminId(Long userId);

    @Select("<script> select id,username,parentId from web_user where id in <foreach item='item' " +
            "collection='userIds' open='('" + " " +
            "separator=',' close=')'>#{item}</foreach> </script>")
    List<User> getUsersByUserIds(List<Long> userIds);
}